package com.hxzy.controller.plotinfo;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.BeanValidatorUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import com.hxzy.entity.PlotInfo;
import com.hxzy.entity.UserInfo;
import com.hxzy.service.PlotService;
import com.hxzy.service.impl.PlotServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/3/2 9:47
 */
@WebServlet(urlPatterns = "/insertJson/plot")
public class InsertJsonPlot extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        try {
            PlotInfo plotInfo = super.CollectiongParameterToJSON(req, PlotInfo.class);
            Set<String> validator = BeanValidatorUtil.validator(UserInfo.class, plotInfo);
            if (validator.isEmpty()){
                PlotService service=new PlotServiceImpl();
                int insert = service.insert(plotInfo);
                ResultAjax resultAjax;
                if (insert>0){
                    resultAjax=ResultAjax.success("加入成功");
                }else {
                    resultAjax= ResultAjax.error("加入失败");
                }
                super.render(resp, JSONObject.toJSONString(resultAjax));
            }else {
                String next = validator.iterator().next();
                ResultAjax resultAjax = ResultAjax.error(next);
                super.render(resp, JSONObject.toJSONString(resultAjax));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
